import json

from DBUtility import DBManage
import redis
from threading import Thread, RLock


def a():
    while True:
        redis_db = redis.StrictRedis()
        db = DBManage()
        try:
            # 从redis数据库中取数据
            data = json.loads(redis_db.lpop(''))
            if data:
                # 获取表名
                one = data['one_classify']
                try:
                    db.create_table(one)
                except Exception as e:
                    print(f"表 {one} 已经被创建")
                with RLock():
                    db.add_data(one, data)
            else:
                break
        except Exception as e:
            print(e)


threading = []
for _ in range(5):
    x = Thread(target=a)
    x.start()
    threading.append(x)
for t in threading:
    t.join()
